package model;

/**
 * Created by eleven on 16/1/3.
 */
public class ControllerModel {


    public String basisSave(String name, String savename, String daoname, String servicename){
        String savetext =
                "     /**\n" +
                "     * "+name+"\n" +
                "     * @param start\n" +
                "     * @param end\n" +
                "     * @return\n" +
                "     * @throws IOException\n" +
                "     */\n" +
                "    @RequestMapping(value = \"/download"+savename+".do\", method = RequestMethod.GET)\n" +
                "    public ResponseEntity<byte[]> download"+savename+"(\n" +
                "            @RequestParam(value = \"start\", required = false) @DateTimeFormat(pattern = \"yyyy-MM-dd\") Date start,\n" +
                "            @RequestParam(value = \"end\", required = false) @DateTimeFormat(pattern = \"yyyy-MM-dd\") Date end\n" +
                "    ) throws IOException {\n" +
                "        Date end_date = end == null ? new Date(System.currentTimeMillis()) : end;\n" +
                "        Date start_date = start == null ? DateUtils.addDays(end, -3) : start;\n" +
                "\n" +
                "        List<"+daoname+"> list = "+servicename+".get"+daoname+"(start_date, end_date);\n" +
                "        HttpHeaders headers = new HttpHeaders();\n" +
                "        String file = DateUtil.formatDate(new Date(), \"yyyyMMddHHmmss\");\n" +
                "        String fileName = new String((file + \".csv\").getBytes(\"UTF-8\"), \"iso-8859-1\");//为了解决中文名称乱码问题\n" +
                "        headers.setContentDispositionFormData(\"attachment\", fileName);\n" +
                "        headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);\n" +
                "        return new ResponseEntity<byte[]>(CsvWriter.print(list), headers, HttpStatus.CREATED);\n" +
                "    }";
        return savetext;
    }

    public String basisDispatcht(String name,String dispatchname, String daoname, String servicename,String url){
//        String[] typename = dispatchpath.split("/");
        String dispatchtext =
                "     /**\n" +
                "     *"+name+"\n" +
                "     *\n" +
                "     * @param range\n" +
                "     * @return\n" +
                "     */"+
                "    @RequestMapping(\"/"+dispatchname+".htm\")\n" +
                "    public ModelAndView "+dispatchname+"(@ModelAttribute DateRange range){\n" +
                "        Date end_date = range.getEnd() == null ? new Date(System.currentTimeMillis()) : range.getEnd();\n" +
                "        Date start_date = range.getStart() == null ? DateUtils.addDays(end_date, -3) : range.getStart();\n" +
                "        List<"+daoname+"> list = "+servicename+".get"+daoname+"(start_date, end_date);\n" +
                "\n" +
                "        Map<String, Object> map = new HashMap<String, Object>();\n" +
                "        map.put(\"start\", DateUtil.formatDate(start_date, \"yyyy-MM-dd\"));\n" +
                "        map.put(\"end\", DateUtil.formatDate(end_date, \"yyyy-MM-dd\"));\n" +
                "        map.put(\"data\", list);\n" +
                "        map.put(\"ruleType\", \""+dispatchname+"\");\n" +
                "\n" +
                "        ModelAndView mv = super.modelAndView(map);\n" +
                "        mv.setViewName(Const."+url+");\n" +
                "        return mv;\n" +
                "\n" +
                "    }";
        return dispatchtext;
    }
}
